﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ZH.GPT.Domains.User.Request;
using ZH.GPT.Domains.User.Response;
using ZH.GPT.Domains;
using ZH.GPT.Services.Interface;
using ZH.GPT.Utils;
using ZH.GPT.Version;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static ZH.GPT.Version.CustomApiVersion;
using System.Net;
using ZH.RetailContainer.CommLibrary.Result;

namespace ZH.GPT.Controllers.v1
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    //[ApiExplorerSettings(GroupName = "v1")]
    //[Route("api/v1/user")]

    //和上边的版本控制以及路由地址都是一样的
    [CustomRoute(ApiVersions.V1, "User")]
    public class UserController : BaseController
    {
        private readonly ILogger<UserController> logger;
        IUserService userService;
        /// <summary>
        /// 用户构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="userService">用户服务</param>
        public UserController(ILogger<UserController> logger, IUserService userService)
        {
            this.logger = logger;
            this.userService = userService;
        }

        /// <summary>
        /// 获取用户名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetUserAsync")]
        [ProducesResponseType(typeof(Result<UserPDTO>), 200)]
        public async Task<ContentResult> GetUserAsync(Guid id)
        {
            UserPDTO user = await userService.GetUserAsync(id);
            return Success(user);
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="user">用户实体</param>
        /// <returns></returns>
        [HttpPost]
        [Route("ModifyUserAsync")]
        [ProducesResponseType(typeof(Result<object>), 200)]
        [ProducesResponseType(typeof(Result<Error>), (int)ErrorCode.User_Null)]
        public async Task<ContentResult> ModifyUserAsync([FromBody] UserQDTO user)
        {
            logger.LogError($"ZH.GPT_TestController_TestNoAuthorize_Start:\r\n{user}");
            //异常需要返回异常结构，这里null判断只是做个例子
            //用于业务是否成功
            bool isSucceed = false;
            //此处的sign是一个标识，表示执行的位置，根据日志位置去写，和前面的Start一个道理。
            logger.LogError($"ZH.GPT_TestController_TestNoAuthorize_Sign:\r\n{user}");
            if (user == null)
                return Error(ErrorCode.User_Null);

            logger.LogError($"ZH.GPT_TestController_TestNoAuthorize_End:\r\n{isSucceed}");//一般的查询接口不需要输出结果集（比如查询下订单列表，订单详情等，因为输出值可以直接拿到），只需要对新增、修改、删除等改变数据接口记录结果，及一些很关键的查询接口。
            //成功的话直接用PackResult.Pack()返回成功状态
            return await Task.FromResult<ContentResult>(Success());
        }


        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("AddUserBatchAsync")]
        [ProducesResponseType(typeof(Result<object>), 200)]
        public async Task<ContentResult> AddUserBatchAsync()
        {
            int count = await userService.AddUserBatchTestAsync();
            //成功的话直接用PackResult.Pack()返回成功状态
            return Success();
        }



    }

   
}
